package com.augmentum.login.dao.impl.mybatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.augmentum.login.Constants;
import com.augmentum.login.dao.BaseDao;
import com.augmentum.login.dao.QuestionDao;
import com.augmentum.login.model.Page;
import com.augmentum.login.model.Question;
import com.augmentum.login.util.SqlMethod;

@Repository("questionDao")
public class QuestionDaoImpl extends BaseDao implements QuestionDao {

    @Override
    public Question getQuestionById(String questionId) {
        return getSqlSession().selectOne(
                SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_QUERY_BY_ID.getValue(), questionId);
    }

    @Override
    public void deleteQuestion(String questionId) {
        getSqlSession().delete(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_DELETE.getValue(), questionId);
    }

    @Override
    public void deleteBatchQuestions(String[] questionIds) {
        getSqlSession().delete(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_DELETE_BATCH.getValue(),
                questionIds);
    }
    
    @Override
    public List<Integer> getQuestionCountForRandom(int count) {
        return getSqlSession().selectList(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_COUNT_FOR_RANDOM.getValue(), count);
    }
    
    @Override
    public void updateState(String[] questionIds) {
        getSqlSession().update(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_UPDATE_STATE.getValue(),
                questionIds);
    }

    @Override
    public void updateQuestion(Question question) {
        getSqlSession().update(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_UPDATE.getValue(), question);
    }

    @Override
    public void createQuestion(Question question) {
        getSqlSession().insert(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_CREATE.getValue(), question);
    }

    @Override
    public List<Question> findQuestionList() {
        return getSqlSession()
                .selectList(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_QUERY_LIST.getValue());
    }

    @Override
    public List<Question> findQuestionsByPage(String description, String order, Page page) {

        if (page.getCurrentPage() > page.getPageCount()) {
            page.setCurrentPage(page.getPageCount());
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Constants.DESCRIPTION, description);
        map.put(Constants.ORDER, order);
        map.put(Constants.OFFSET, page.getOffSet());
        map.put(Constants.PAGE_SIEZ, page.getPageSize());
        return getSqlSession().selectList(
                SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_QUERY_LIST_BY_PAGE.getValue(), map);
    }

    @Override
    public int getQuestionCount(String description) {
        return getSqlSession().selectOne(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_COUNT.getValue(),
                description);
    }

    @Override
    public List<Question> findQuestionById(String questionId) {
        return getSqlSession().selectList(
                SqlMethod.QUESTION_NAME.getValue() + SqlMethod.QUESTION_FIND_BY_ID.getValue(), questionId);
    }

    @Override
    public Question getById(int id) {
        return getSqlSession().selectOne(SqlMethod.QUESTION_NAME.getValue() + SqlMethod.GET_BY_ID.getValue(), id);
    }

}